﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Editor : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public string Filename;

        private bool needsSaving_;
        public bool NeedsSaving
        {
            get { return needsSaving_; }
            private set
            {
                needsSaving_ = value;
                UpdateTitle();
            }
        }

        public Editor()
        {
            NeedsSaving = false;
            InitializeComponent();

            //scintilla1.TextInserted += new EventHandler<ScintillaNet.TextModifiedEventArgs>(scintilla1_TextInserted);
            //scintilla1.TextDeleted += new EventHandler<ScintillaNet.TextModifiedEventArgs>(scintilla1_TextDeleted);
            scintilla1.ConfigurationManager.Language = "cpp";
            scintilla1.Margins[0].Width = 20;
            scintilla1.MarginClick += new EventHandler<ScintillaNet.MarginClickEventArgs>(scintilla1_MarginClick);
            scintilla1.Commands.RemoveBinding(Keys.Z, Keys.Control, ScintillaNet.BindableCommand.Undo);
            scintilla1.KeyUp += new KeyEventHandler(scintilla1_KeyUp);
            scintilla1.BeforeTextInsert += new EventHandler<ScintillaNet.TextModifiedEventArgs>(scintilla1_BeforeTextInsert);
            scintilla1.BeforeTextDelete += new EventHandler<ScintillaNet.TextModifiedEventArgs>(scintilla1_BeforeTextDelete);
            //scintilla1.Commands.AddBinding(Keys.Z, Keys.Control, ScintillaNet.BindableCommand.us

            FormClosing += new FormClosingEventHandler(Editor_FormClosing);
        }

        void scintilla1_BeforeTextDelete(object sender, ScintillaNet.TextModifiedEventArgs e)
        {
            NeedsSaving = !e.UndoRedoFlags.IsLastStep;
            System.Diagnostics.Debug.WriteLine("BeforeDelete");
        }

        void scintilla1_BeforeTextInsert(object sender, ScintillaNet.TextModifiedEventArgs e)
        {
            NeedsSaving = !e.UndoRedoFlags.IsLastStep;
            System.Diagnostics.Debug.WriteLine("BeforeInsert");
        }

        //void scintilla1_TextDeleted(object sender, ScintillaNet.TextModifiedEventArgs e)
        //{
        //    NeedsSaving = !e.UndoRedoFlags.IsLastStep;
        //}

        //void scintilla1_TextInserted(object sender, ScintillaNet.TextModifiedEventArgs e)
        //{
        //    NeedsSaving = !e.UndoRedoFlags.IsLastStep;
        //}
        
        void Editor_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (NeedsSaving)
            {
                MessageBox.Show("Save me");
            }
        }

        void scintilla1_TextChanged(object sender, EventArgs e)
        {
        }

        void scintilla1_MarginClick(object sender, ScintillaNet.MarginClickEventArgs e)
        {
        }

        void scintilla1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Control && e.KeyCode == Keys.Z)
            {
                scintilla1.UndoRedo.Undo();
                NeedsSaving = scintilla1.UndoRedo.CanUndo;
                System.Diagnostics.Debug.WriteLine("Undo");
            }
            //if (e.Control && e.KeyValue == 'C')
            //{
            //    if (scintilla1.Selection.Length == 0)
            //    {
            //        int begin = scintilla1.CurrentPos;
            //        while (begin >= 0 && scintilla1.Text[begin] != '\n' && scintilla1.Text[begin] != '\r') begin--; ;
            //        int end = scintilla1.CurrentPos;
            //        while (end < scintilla1.Text.Length && scintilla1.Text[end] != '\r' && scintilla1.Text[end] != '\r') end++;
            //        scintilla1.Clipboard.Copy(begin + 1, end - 1);
            //    }
            //}

            //if (e.Control && e.KeyValue == 'X')
            //{
            //    if (scintilla1.Selection.Length == 0)
            //    {
            //        int begin = scintilla1.CurrentPos-1;
            //        while (begin > 0 && scintilla1.Text[begin] != '\n' && scintilla1.Text[begin] != '\r') begin--; ;
            //        int end = scintilla1.CurrentPos;
            //        while (end < scintilla1.Text.Length && scintilla1.Text[end] != '\r' && scintilla1.Text[end] != '\r') end++;

            //        scintilla1.Selection.Start = begin;
            //        scintilla1.Selection.End = end;
            //        scintilla1.Clipboard.Cut();
            //    }
            //}
        }

        public void LoadFile(string filename)
        {
            Filename = filename;
            scintilla1.NativeInterface.SetText(System.IO.File.ReadAllText(Filename));
            //scintilla1.NativeInterface.
            //scintilla1.InsertText();
            scintilla1.UndoRedo.EmptyUndoBuffer();
            NeedsSaving = false;
            Activate();
        }

        public void GotoLine(int line)
        {
            scintilla1.GoTo.Line(line);
        }

        private void Editor_Resize(object sender, EventArgs e)
        {
            scintilla1.Top = 0;
            scintilla1.Left = 0;
            scintilla1.Height = ClientSize.Height;
            scintilla1.Width = ClientSize.Width;
        }

        private void UpdateTitle()
        {
            if (string.IsNullOrEmpty(Filename)) Text = "Untitled";
            else Text = System.IO.Path.GetFileName(Filename);
            if (NeedsSaving) Text += " *";
        }

        public bool PromptForFilename
        {
            get { return NeedsSaving && string.IsNullOrEmpty(Filename); }
        }

        public void SaveFile()
        {
            NeedsSaving = false;
            System.IO.File.WriteAllText(Filename, scintilla1.Text);
        }
    }

    class MyScintilla : ScintillaNet.Scintilla
    {
        protected override void WndProc(ref Message m)
        {
            System.Diagnostics.Debug.WriteLine(m.LParam.ToString() + ", " + m.WParam.ToString());
            base.WndProc(ref m);
        }
    }
}
